package com.whisper.view;

import com.whisper.utils.CommonUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Tray {

    public void paintTray(JFrame jFrame) {
        ImageIcon img = new ImageIcon(Tray.class.getClassLoader().getResource("img/logo.jpg"));
        jFrame.setIconImage(img.getImage());

        //得到当前系统托盘
        SystemTray systemtray = SystemTray.getSystemTray();

        //定义弹出菜单
        PopupMenu pm = new PopupMenu();

//        //定义弹出菜单项
//        MenuItem openmenu = new MenuItem("打开主面板");
        MenuItem closeMenu = new MenuItem(CommonUtils.getUTF8String("exit"));
//        MenuItem online = new MenuItem("我在线上");
//        MenuItem busy = new MenuItem("忙碌");
//        MenuItem invisible= new MenuItem("隐身");

//        //添加弹出菜单项到弹出菜单
//        pm.add(openmenu);
        pm.add(closeMenu);
//        pm.add(online);
//        pm.add(busy);
//        pm.add(invisible);

        //创建带指定图像、工具提示和弹出菜单的 TrayIcon
        TrayIcon trayicon = new TrayIcon(img.getImage(),"whisper",pm);

        //将TrayIcon添加到系统托盘
        try {
            systemtray.add(trayicon);
        } catch (AWTException e) {
            e.printStackTrace();
        }

        //为trayicon添加鼠标监听器
        trayicon.addMouseListener(new MouseAdapter(){

            public void mouseClicked(MouseEvent e) {

                //鼠标左键点击,设置窗体状态，正常显示
                if(e.getButton()==MouseEvent.BUTTON1){
                    jFrame.setExtendedState(JFrame.NORMAL);
                }
            }
        });

//        jFrame.setType(Window.Type.UTILITY);
//        jFrame.setUndecorated(true);

        //设置单击击系统托盘图标显示主窗口
        trayicon.addMouseListener(new MouseAdapter(){

            public void mouseClicked(MouseEvent e) {

                //鼠标左键点击,设置窗体状态，正常显示
                if(e.getButton()==MouseEvent.BUTTON1){
                    jFrame.setExtendedState(JFrame.NORMAL);
                    jFrame.setVisible(true);
                }
            }
        });
    }
}
